package project.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Users.Client;
import schronisko.Queries;

/**
 * Servlet implementation class Notes
 */
@WebServlet("/Notes")
public class Notes extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
	Queries q = new Queries();
	Client c;
	HttpSession s;
    public Notes() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		s = request.getSession();
		String type = request.getParameter("type");
		switch(type){
		case "submit":
			int ID = Integer.parseInt(request.getParameter("id"));
			String text = request.getParameter("msg");
			int note = Integer.parseInt(request.getParameter("note"));
			int suc = q.makeNote(ID, text, note);
			if(suc == 1) {
				s.setAttribute("noted", 1); // pomyslnie
				response.sendRedirect("user/make_note.jsp");
			}
			else if(suc == 0){
				s.setAttribute("noted", 2); // problem
				response.sendRedirect("user/make_note.jsp");
			}
			else if(suc == -1){
				s.setAttribute("noted", 3); // baza nie odpowiada
				response.sendRedirect("user/make_note.jsp");
			}
			break;
		}
	}

}
